package www.rlicker.controller;

import java.io.File;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import www.rlicker.model.Ueditor;
import www.rlicker.utils.DateUtil;
import www.rlicker.utils.PublicMsg;


@Controller  
public class UEditorController {
	
	@RequestMapping(value="/ueditor")
    @ResponseBody
    public String ueditor(HttpServletRequest request) {
        return PublicMsg.UEDITOR_CONFIG;
    }
	
	/***
	 * 娑撳﹣绱堕弬鍥︽
	 * @param upfile
	 * @param request
	 * @return
	 */
/*	@RequestMapping(value = "/downFile", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, String> downFile(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse resp) throws FileUploadException {
		Map<String, String> msgg= new HashMap<String, String>();
        if (file.isEmpty()) {
        	msgg.put("msg", "閺傚洣娆㈡稉宥堝厴娑撹櫣鈹栭敍锟�");
            return msgg;
           
        }else {
        	List<DownloadFile> queryBy = downloadFileService.queryBy(request.getParameter("publishId"));
        	if(queryBy.size()>=3){
        		msgg.put("msg", "閸欘亣鍏樺ǎ璇插娑撳娼�");
                return msgg;
        	}
        		 File targetFile=null;
    	    	 File targetFile2=null;
    	    	  String fileName=file.getOriginalFilename();//閼惧嘲褰囬弬鍥︽閸氬秴濮為崥搴ｇ磻
    	    	  String asd= "C:/Program Files/apache-tomcat-7.0.88/webapps/itv-admin/WEB-INF/classes";
    	       	if(file.getSize()>1024*1024*30){
    	       		msgg.put("msg", "閺傚洣娆㈡稉宥堝厴鐡掑懓绻�30M");
    	  	        return msgg;
    	         	}
    	        if(fileName!=null&&fileName!=""){   
    	            String path =asd+"/static/upload/";  //閺傚洣娆㈢�涙ê鍋嶆担宥囩枂
    	           String zxc= "C:/Program Files/upload";//閺傚洣娆㈡径鍥﹀敜鐎涙ê鍋嶆担宥囩枂
    	            String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//閺傚洣娆㈤崥搴ｇ磻
    	            fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;//閺傛壆娈戦弬鍥︽閸氾拷
    	            //閸忓牆鍨介弬顓熸瀮娴犺埖妲搁崥锕�鐡ㄩ崷锟�
    	            String fileAdd = DateUtil.formatDate(new Date(),"yyyyMMdd");
    	            File file2 =new File(zxc+"/"+fileAdd);
    	               //婵″倹鐏夐弬鍥︽婢堕�涚瑝鐎涙ê婀崚娆忓灡瀵わ拷    
    	               if(!file2 .exists()  && !file2 .isDirectory()){       
    	                   file2 .mkdir();  
    	               }
    	            File file1 =new File(path+"/"+fileAdd); 
    	            //婵″倹鐏夐弬鍥︽婢堕�涚瑝鐎涙ê婀崚娆忓灡瀵わ拷    
    	            if(!file1 .exists()  && !file1 .isDirectory()){       
    	                file1 .mkdir();  
    	            }
                	targetFile2 = new File(file2, fileName);
    	            targetFile = new File(file1, fileName);
    	            try {
    	            	  //	file.transferTo(targetFile2);
    	            	file.transferTo(targetFile);
    	            	DownloadFile downloadFile = new DownloadFile(); 
    	            	downloadFile.setDownloadId(MyUtils.getSeqNum());
    	            	downloadFile.setCreateTime(DateUtil.getCurrentDate());
    	            	downloadFile.setDowloadText(request.getParameter("fileText"));
    	            	downloadFile.setPublishId(request.getParameter("publishId"));
    	            	downloadFile.setPayImgTextId(request.getParameter("payImgTextId"));
    	            	downloadFile.setDownloadContent(path+fileAdd+"/"+fileName);
    	            	String insert = downloadFileService.insert(downloadFile);
    	            	if(insert!=null){
    	            		msgg.put("msg", insert);
    	            		  return msgg;
    	            	}
    	            	   msgg.put("state","SUCCES");
    	            } catch (Exception e) {
    	                e.printStackTrace();
    	            }
    	        }
    	        }
     
        return msgg;

	}*/
	  
	  
	/***
	 * @param upfile
	 * @param request
	 * @return
	 */
	    @RequestMapping(value="/videoUpload")
	    @ResponseBody
	    public Ueditor videoUpload(@RequestParam("upfile")MultipartFile upfile,HttpServletRequest request) {
	    	 File targetFile=null;
	         String msg="";
	    	 File targetFile2=null;
	    	  String fileName=upfile.getOriginalFilename();
	        Ueditor ueditor = new Ueditor();
	       	if(upfile.getSize()>1024*1024*30){
	         	  ueditor.setState("0M");
	  	        return ueditor;
	         	}
	        if(fileName!=null&&fileName!=""){   
	       	    String path= request.getServletContext().getRealPath("/static/files/");
	            String zxc= "C:/Program Files";
	            String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());
	            fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;
	            String fileAdd = DateUtil.formatDate(new Date(),"yyyyMMdd");
	            File file2 =new File(zxc+"/"+fileAdd);
	               if(!file2 .exists()  && !file2 .isDirectory()){       
	                   file2 .mkdir();  
	               }
	            File file1 =new File(path+"/"+fileAdd); 
	            if(!file1 .exists()  && !file1 .isDirectory()){       
	                file1 .mkdir();  
	            }
            	targetFile2 = new File(file2, fileName);
	            targetFile = new File(file1, fileName);
	            try {
	            	//upfile.transferTo(targetFile2);
	             	  	upfile.transferTo(targetFile);
	                msg=request.getScheme()+"://www.tongxiao.vip/"+request.getContextPath()+"/"+"static/files/"+fileAdd+"/"+fileName;
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	            ueditor.setUrl(msg);
	            ueditor.setState("SUCCESS");
	            ueditor.setOriginal(upfile.getOriginalFilename());
	            ueditor.setTitle(upfile.getOriginalFilename());
	            return ueditor;
	        }
	        ueditor.setState("FALSE");
	        return ueditor;
	    }
	  /***
		 * @param upfile
		 * @param request
		 * @return
		 */
	  @RequestMapping(value="/fileUpload")
	    @ResponseBody
	    public Ueditor fileUpload(@RequestParam("upfile")MultipartFile upfile,HttpServletRequest request) {
	    	 File targetFile=null;
	    	 File targetFile2=null;
	         String msg="";
	    	  String fileName=upfile.getOriginalFilename();
	       	  String path= request.getServletContext().getRealPath("/static/files/");
	        Ueditor ueditor = new Ueditor();
	       	if(upfile.getSize()>1024*1024*30){
	         	  ueditor.setState("30M");
	  	        return ueditor;
	         	}
	        if(fileName!=null&&fileName!=""){   
	            String zxc= "C:/Program Files";
	            String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());
	            fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;

	            String fileAdd = DateUtil.formatDate(new Date(),"yyyyMMdd");
	            File file2 =new File(zxc+"/"+fileAdd);
	               if(!file2 .exists()  && !file2 .isDirectory()){       
	                   file2 .mkdir();  
	               }
	            File file1 =new File(path+"/"+fileAdd); 
	            if(!file1 .exists()  && !file1 .isDirectory()){       
	                file1 .mkdir();  
	            }
	            	targetFile2 = new File(file2, fileName);
	               targetFile = new File(file1, fileName);
	               try {
	               	upfile.transferTo(targetFile);
	                //  	upfile.transferTo(targetFile2);
	                msg=request.getScheme()+"://www.tongxiao.vip/"+request.getContextPath()+"/"+"static/files/"+fileAdd+"/"+fileName;
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	            ueditor.setUrl(msg);
	            ueditor.setState("SUCCESS");
	            ueditor.setOriginal(upfile.getOriginalFilename());
	            ueditor.setTitle(upfile.getOriginalFilename());
	            return ueditor;
	        }
	        ueditor.setState("FALSE");
	        return ueditor;
	    }
	  protected boolean verifyUploadFile(String name)
		{
			for (String ext : uploadExtension.split(","))
			{
				if (ext.charAt(0) != '.') ext = '.' + ext;
				if (name.endsWith(ext)) return true;
			}
			return false;
		}
		
		
		private String uploadExtension = "jpg,png,gif,bmp,jpeg";
	  /***
	   * @param upfile
	   * @param request
	   * @return
	   */
    @RequestMapping(value="/imgUpload")
    @ResponseBody
    public Ueditor imgUpload(@RequestParam("upfile")MultipartFile upfile,HttpServletRequest request) {
        Ueditor ueditor = new Ueditor();
        String msg="";
    	if (upfile.isEmpty()) {
    		  ueditor.setState("FALSE");
    	        return ueditor;

         }else{
        	if(!verifyUploadFile(upfile.getOriginalFilename())){
        			  ueditor.setState("FALSE");
          	        return ueditor;
            	}
        	 File targetFile=null;
        	 File targetFile2=null;
       	  String fileName=upfile.getOriginalFilename();
       	if(upfile.getSize()>1024*1024*30){
       	  ueditor.setState("30M");
	        return ueditor;
       	}
           if(fileName!=null&&fileName!=""){   
        	   String path = request.getServletContext().getRealPath("/static/image");
               String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());
               fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;
               File file1 =new File(path); 
               if(!file1 .exists()  && !file1 .isDirectory()){       
                   file1 .mkdir();  
               }
               targetFile = new File(file1, fileName);
               try {
               	upfile.transferTo(targetFile);
                msg=request.getScheme()+"://www.tongxiao.vip/"+request.getContextPath()+"/"+"static/image/"+fileName;
               } catch (Exception e) {
                   e.printStackTrace();
               }
               ueditor.setUrl(msg);
               ueditor.setState("SUCCESS");
               ueditor.setOriginal(upfile.getOriginalFilename());
               ueditor.setTitle(upfile.getOriginalFilename());
               return ueditor;
           } 
         }
    	
        ueditor.setState("FALSE");
        return ueditor;
    }
	
}
